﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Runtime.Hosting;
using System.Workflow.ComponentModel;

using HT.Model;
using HT.Persistency;
using HT.Workflow;
using HT.Service;

namespace HT.Business
{
    public class ReimbursementHandler
    {
        /// <summary>
        /// 创建一个报销工作流实例
        /// </summary>
        public void CreateReimbursementWorkflow(string owner, double account, string reason)
        {
            ReimbursementInfo reimbursementInfo = new ReimbursementInfo();
            reimbursementInfo.Owner = owner;
            reimbursementInfo.Account = account;
            reimbursementInfo.Reason = reason;

            Dictionary<string, object> inputParameters = new Dictionary<string, object>();
            inputParameters.Add("ReimbursementInfoProperty", reimbursementInfo);

            WorkflowEngine.GetWorkflowEngineInstance().CreateWorkflow(typeof(ReimbursementWorkflow), inputParameters);
        }

        /// <summary>
        /// 穷举指定用户的报销流程数据
        /// </summary>
        public List<ReimbursementInfo> SearchMyReimbursementInfo(string userId)
        {
            ReimbursementPersistency reimbursementPersistency = new ReimbursementPersistency();
            List<ReimbursementInfo> reimbursementInfoList = reimbursementPersistency.GetReimbursementInfoListByOwner(userId);

            WorkflowRuntime workflowRuntime = WorkflowEngine.GetWorkflowEngineInstance().GetWorkflowRuntime();
            foreach (ReimbursementInfo reimbursementInfo in reimbursementInfoList)
            {
                StateMachineWorkflowInstance smwi = new StateMachineWorkflowInstance(workflowRuntime, reimbursementInfo.InstanceID);

                //从工作流跟踪信息中获得工作流当前状态                    
                reimbursementInfo.CurrentStatus = smwi.CurrentStateName;
            }

            return reimbursementInfoList;
        }

        /// <summary>
        /// 穷举需指定用户处理的报销流程数据
        /// </summary>
        public List<ReimbursementInfo> SearchWaitingReimbursementInfo(string userId)
        {    

            ReimbursementPersistency reimbursementPersistency = new ReimbursementPersistency();
            List<ReimbursementInfo> reimbursementInfoList = reimbursementPersistency.GetWaitingReimbursementInfoListByUser(userId);
            
            return reimbursementInfoList;
        }

        /// <summary>
        /// 触发报销流程事件
        /// </summary>
        public void RaiseReimbursementEvent(Guid instanceID, int eventname, string comments)
        {
            ReimbursementServiceImpl reimbursementService = (ReimbursementServiceImpl)WorkflowEngine.GetWorkflowEngineInstance().GetWorkflowRuntime().GetService(typeof(ReimbursementServiceImpl));
           
            ReimbursementPersistency reimbursementPersistency = new ReimbursementPersistency();
            ReimbursementInfo reimbursementInfo = reimbursementPersistency.GetReimbursementInfoByInstanceID(instanceID);

            if (eventname.Equals(ReimbursementEvent.RMApproveEvent))
            {
                reimbursementInfo.RMSuggestion = comments;
                reimbursementService.RaiseApproveEvent(reimbursementInfo);
            }
            else if (eventname.Equals(ReimbursementEvent.RMRejectEvent))
            {
                reimbursementInfo.RMSuggestion = comments;
                reimbursementService.RaiseRejectEvent(reimbursementInfo);
            }
            else if (eventname.Equals(ReimbursementEvent.ACApproveEvent))
            {                
                reimbursementInfo.ACSuggestion = comments;
                reimbursementService.RaiseApproveEvent(reimbursementInfo);
            }
            else if (eventname.Equals(ReimbursementEvent.ACRejectEvent))
            {
                reimbursementInfo.ACSuggestion = comments;
                reimbursementService.RaiseRejectEvent(reimbursementInfo); 
            }
           
            else if (eventname.Equals(ReimbursementEvent.VPApproveEvent))
            {
                reimbursementInfo.VPSuggestion = comments;
                reimbursementService.RaiseApproveEvent(reimbursementInfo);
            }
            else if (eventname.Equals(ReimbursementEvent.VPRejectEvent))
            {
                reimbursementInfo.VPSuggestion = comments;
                reimbursementService.RaiseRejectEvent(reimbursementInfo);
            }
            else if (eventname.Equals(ReimbursementEvent.PRApproveEvent))
            {
                reimbursementInfo.PRSuggestion = comments;
                reimbursementService.RaiseApproveEvent(reimbursementInfo);
            }
            else if (eventname.Equals(ReimbursementEvent.PRRejectEvent))
            {
                reimbursementInfo.PRSuggestion = comments;
                reimbursementService.RaiseRejectEvent(reimbursementInfo);
            }
 
        }
     

    }
}
